﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Configuration;

namespace mal.Web.Routing
{
    public class RouteConfigurationManager
    {
        public static void Initialize()
        {
            Initialize("RouteConfigurationGroup/RouteConfiguration");
        }
        public static void Initialize(string configurationSection)
        {
            RouteCollection routes = RouteTable.Routes; 
            RouteSection config = (RouteSection)ConfigurationManager.GetSection(configurationSection);

            foreach (RouteElement item in config.Routes)
            {

                Route route = new Route(item.Uri, new PageRouteHandler(item.MappedUri));
                
                if(item.Defaults.Count>0)
                {
                    RouteValueDictionary defaultValues = new RouteValueDictionary();
                    foreach (RouteElementValue defaultItem in item.Defaults)
	                {
                        defaultValues.Add(defaultItem.Name,defaultItem.Value);
	                }

                    route.Defaults = (defaultValues);
                }

                if(item.Constraints.Count>0)
                {
                    RouteValueDictionary contraintValues = new RouteValueDictionary();
                    foreach (RouteElementValue contraintItem in item.Constraints)
	                {
                        contraintValues.Add(contraintItem.Name,contraintItem.Value);
	                }

                    route.Constraints = (contraintValues);
                }

                routes.Add(item.Name,route);
                

                //Dictionary<string, string> defaultValues = new Dictionary<string, string>();
                //defaultValues.Add("culture", "en-US");

                //string param1 = "culture";
                //string value1 = "en-US";

                //RouteValueDictionary defaults = new RouteValueDictionary();
                //defaults.Add(param1, value1);

                //routes.Add(item.Name, new Route(item.Uri, new PageRouteHandler(item.MappedUri))
                //{
                //    Defaults = defaults
                //});

              
            }
        }
    }
}
